#!/bin/bash

# if error, exit immediately
set -e

# check required tools
REQUIRED_TOOLS=("qemu-img" "parted" "grub-install" "mount" "losetup")
for tool in "${REQUIRED_TOOLS[@]}"; do
    if ! command -v "$tool" &> /dev/null; then
        echo "install required tool: $tool..."
        apt-get update && apt-get install -y "$tool"
    fi
done

# config params
# builder sys work dir
WORK_DIR=$(cd "$(dirname "$0")" && pwd -P)
echo "builder sys work dir: ${WORK_DIR}"
# Target directory
TARGET_DIR=$(dirname "$(pwd)")
# Target tmp dir
TMP_DIR="${TARGET_DIR}/tmp"
# Target img dir
IMG_DIR="${TARGET_DIR}/img"

# Target work directory
if [ ! -d ${TARGET_DIR} ]; then
    mkdir -p ${TARGET_DIR} 
fi

# Target tmp directory
if [ ! -d ${TMP_DIR} ]; then
    mkdir -p ${TMP_DIR}
fi

# Target img directory
if [ ! -d ${IMG_DIR} ]; then
    mkdir -p ${IMG_DIR}
fi

# pkg initramfs
source ${WORK_DIR}/pkg_initramfs_amd64.sh
# Build prod boot image:legacy
source ${WORK_DIR}/boot_img_legacy_amd64.sh
# Build prod boot image:uefi
#source ${WORK_DIR}/boot_img_uefi_amd64.sh
